<?php
	function db_process_comment($_POST,$type)
	{
		$yourname 		 = $_POST['fullname'];
		$youremail 		 = $_POST['email'];
		$yourmessage	 = $_POST['message'];
		$yourcaptcha	 = $_POST['captcha'];
				
		$yoursession	 = $_SESSION['ma_xac_nhan'];		

		$mailto 		 = db_get_mail_admin_managerment("mySetting_company_email");		
		## delace prototype
		$db_session	= array();
		$db_error	= array();
		
		if($yourname)			$db_session['yourname']		=	$yourname;
		if($youremail)			$db_session['email']		= 	$youremail;
		if($yourmessage)		$db_session['yourmessage']	= 	$yourmessage;
		if($_POST['captcha'])	$db_session['captcha']		= 	$yourcaptcha;
		
		$_SESSION['contact']	=	$db_session;
		
		if($yoursession!= $_POST['captcha'])
		{
			$_SESSION['contact']['captcha']	= $_POST['captcha'];
			$db_error[] = 'The code invalid!';
		}
		if(!$db_error)
		{
				### OK, nếu email này chưa là thành viên
				if(is_email($_POST['email']) && !email_exists($_POST['email']))
				{
					### đăng ký thành viên cho user này					
					$user_id	= db_insert_user_comment($_POST);
					if(is_int($user_id) && $user_id>0)
					{
						### lưu comment
						ok_add_comment(get_the_ID(), $_POST['fullname'], $_POST['email'], $_POST['message'], $user_id,$type);
						$db_error[]="Thank you for commenting on this article!";
						$_SESSION['contact'] = null;						
					}
				}
				### nếu email này đã là thành viên
				else
				{
					$this_author_ID	=	email_exists($_POST['email']);
					ok_add_comment(get_the_ID(), $_POST['fullname'], $_POST['email'], $_POST['message'], $this_author_ID,$type);
					$db_error[]="Thank you for commenting on this article";	
					$_SESSION['contact'] = null;			
				}	  	
		}	
		return $db_error;
	}
	
	//=============================================================================================
	function ok_add_comment($post_id, $author, $author_email, $content, $user_id,$type)
	{
		//echo $post_id.' - '.$author.' - '.$author_email.' - '.$content.' - '.$user_id;
			$time = current_time('mysql');
			$data = array(
			    'comment_post_ID' => $post_id,
			    'comment_author' => $author,
			    'comment_author_email' => $author_email,
			    'comment_author_url' => 'http://',
			    'comment_content' => $content,		    
			    'comment_parent' => 0,
			    'user_id' => $user_id,
			    'comment_author_IP' => preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ),
			    'comment_agent' => substr($_SERVER['HTTP_USER_AGENT'], 0, 254),
				'comment_type'	=> $type,
			    'comment_date' => $time,
			    'comment_approved' => 0,
			);		
			$comments_id = wp_insert_comment($data);			
	}
	
	## Comment
	function db_insert_user_comment($param)	
	{
	 	global $wpdb;
		if(!email_exists($param['email']))
		{	$password = wp_generate_password(6, false);
					
			$user_login		 	= esc_attr($param['email']);
			$user_pass		 	= $password;			
			$user_email 		= esc_attr($param['email']);
			//$display_name		= $param['fullnames'];
			$role				= 'subscriber';
					
			$user_data = compact('user_login', 'user_email', 'user_pass','role');	
			$user_id = wp_insert_user($user_data);			
			return $user_id;
		}
	}